////////////////////////////////////////////////////////////////////////////////
//
//  FLYING KIWI BV
//  Copyright 2010 Flying Kiwi BV
//  All Rights Reserved.
//
//  NOTICE: Flying Kiwi permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
package plantool.modules.orders.windows.objects {
	//--------------------------------------------------------------------------
	//  Imports
	//--------------------------------------------------------------------------
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import kiwi.components.Button;
	import kiwi.events.CallbackEvent;
	
	import plantool.display.windows.BaseWindow;
	import plantool.display.windows.objects.WindowRow;
	import plantool.helpers.TextFieldHelper;
	import plantool.modules.orders.data.Orderrow;
	import plantool.modules.orders.windows.OrderWindow;
	
	
	//--------------------------------------------------------------------------
	//  [Metadata (Events, Style, Effects etc)]
	//--------------------------------------------------------------------------
	
	//--------------------------------------------------------------------------
	//  Class declaration
	//--------------------------------------------------------------------------
	public class OrderWindowOrderRow extends WindowRow {
		//--------------------------------------------------------------------------
		//Class constants
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Class variables
		//--------------------------------------------------------------------------
		private var _addButton:Button		= null;
		private var _orderRow:Orderrow		= null;
		private var _tfTime:TextField		= null;
		
		//--------------------------------------------------------------------------
		//Class properties
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//Constructor and Destructor
		//--------------------------------------------------------------------------
		public function OrderWindowOrderRow(window:OrderWindow) {
			super(window);
		}
		
		public override function dispose():void {
			//clean up addbutton
			if (_addButton) {
				_addButton.dispose();
				_addButton = null;
			}
			
			//clean up addbutton
			if (_tfTime) {
				_tfTime = null;
			}
		}
		
		//--------------------------------------------------------------------------
		//Class methods
		//--------------------------------------------------------------------------
		private function onChange_data(e:Event):void {
			switch (e.currentTarget) {
				case _tfTime:
					if (!isNaN(parseInt(_tfTime.text))) {
						_tfTime.backgroundColor = 0xDDDDDD;
						_orderRow.time 			= parseInt(_tfTime.text);
					} else {
						_tfTime.backgroundColor = 0xFF0000;
					}
					break;
			}
		}
		
		private function createRowContent():void {
			//clean up addbutton
			if (_addButton) {
				_addButton.dispose();
				_addButton = null;
			}
			
			//add new row to parent
			window.addChild(new OrderWindowOrderRow(window as OrderWindow));
			
			//create orderrow
			_orderRow = new Orderrow();
			(window as OrderWindow).order.orderrows.push(_orderRow);
			
			//create input field for time
			_tfTime				 	= TextFieldHelper.createInputTf(false);
			_tfTime.x				= width / 2;
			_tfTime.y				= (height - _tfTime.height) / 2;
			_tfTime.width			= (width - 20) / 2;
			_tfTime.text			= "";
			_tfTime.addEventListener(Event.CHANGE		, onChange_data, false, 0, true);
			addChild(_tfTime);			
		}
		
		private function onMouse_addButton(e:CallbackEvent):void {
			//check mouseevent
			switch (e.userData.type) {
				case MouseEvent.CLICK:
					//create content
					createRowContent();
					break;
			}
		}
		
		protected override function init():void {
			//super
			super.init();
			
			//override height
			height = 26;
			
			//create the addbutton
			_addButton 				= new Button();
			_addButton.text			= "+";
			_addButton.width 		= 15;
			_addButton.height 		= 15;
			_addButton.x			= 15;
			_addButton.x 			= width - _addButton.width - 15;
			_addButton.y			= (height - _addButton.height) / 2;
			_addButton.mouseHandler = onMouse_addButton;
			addChild(_addButton);
		}
	}
}